class Solution:
    def decodeString(self, s: str) -> str:
        self.stack = []
        res, _ = self.decode_string(s)
        return res
    
    def decode_string(self, s):
        i = 0
        k = 0
        res = ''
        while i < len(s):
            if s[i].isdigit():
                index = s[i:].find('[')
                k = int(s[i: i+index])
                i += index
                continue
            if s[i] == '[':
                inner_res, index = self.decode_string(s[i+1:])
                i += index + 1
                res += k * inner_res
                continue
            if s[i] == ']':
                return res, i+1
            res += s[i]
            i += 1
        return res, i+1

if __name__ == '__main__':
    s = Solution()
    s.decodeString("3[a]2[bc]")
